<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="../res/layui/css/layui.css">
    <style>
        #userInfoDialog, #userEditDialog {
            padding: 20px;
            width: 600px;
            display: none; /*隐藏层*/
        }

        #userInfoDialog p, #userEditDialog p {
            padding: 10px 0;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">用户列表</div>
        <div class="layui-card-body">
            <table id="userlist" lay-filter="userFilter"></table>
        </div>
    </div>
</div>
<!--用户信息对话框-->
<div id="userInfoDialog">
    <table class="layui-table" lay-size="sm">
        <tr>
            <td>头像</td>
            <td><img id="headimg" src="" alt="头像" style="max-width: 100px;"></td>
        </tr>
        <tr>
            <td>用户名</td>
            <td><span id="username"></span></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><span id="password"></span></td>
        </tr>
        <tr>
            <td>手机号</td>
            <td><span id="phone"></span></td>
        </tr>
        <tr>
            <td>注册时间</td>
            <td><span id="regtime"></span></td>
        </tr>
        <tr>
            <td>生日</td>
            <td><span id="birth"></span></td>
        </tr>
        <tr>
            <td>状态</td>
            <td><span id="status"></span></td>
        </tr>
        <tr>
            <td>Salt</td>
            <td><span id="salt"></span></td>
        </tr>
    </table>
</div>

<!--用户编辑对话框-->
<div id="userEditDialog">
    <form class="layui-form" lay-filter="userEditForm">
        <input type="hidden" name="id" value="">
        <div class="layui-form-item">
            <label class="layui-form-label">头像</label>
            <div class="layui-input-block">
                <input type="text" name="img" placeholder="请输入头像URL" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">注册时间</label>
            <div class="layui-input-block">
                <input type="text" name="regtime" placeholder="请输入注册时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">生日</label>
            <div class="layui-input-block">
                <input type="text" name="birth" placeholder="请输入生日" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁用">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">Salt</label>
            <div class="layui-input-block">
                <input type="text" name="salt" placeholder="请输入Salt" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="saveUserEdit">保存</button>
            </div>
        </div>
    </form>
</div>

<script src="../res/layui/layui.js"></script>
<script>
    layui.use(['table', 'layer', 'jquery', 'form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;

        // 渲染表格
        table.render({
            elem: '#userlist',
            url: '/user/list',
            size: 'sm',
            page: {
                limit: 2,
                limits: [2, 5, 10, 15, 20]
            },
            cols: [[
                {field: 'id',title: "ID",hide: true},
                {type: 'checkbox'},
                {title: 'uid', type: 'numbers'},
                {title: '用户名', field: 'username'},
                {title: '密码', field: 'password'},
                {title: '邮箱', templet: '<div>{{d.email ? d.email : "暂无"}}</div>'},
                {title: '手机号', templet: '<div>{{d.phone ? d.phone : "暂无"}}</div>'},
                {title: '状态', templet: function (d) {
                        if (d.status === 0){
                            return '<span class="layui-badge layui-bg-green">正常</span>'
                        }else if (d.status === 1){
                            return '<span class="layui-badge layui-bg-red">禁用</span>'
                        }
                    }},
                {title: '操作', templet: '#rowTools'}
            ]],
            toolbar: '#toolbar'
        });

        // 监听工具条
        table.on('tool(userFilter)', function (obj) {
            var data = obj.data; // 获得当前行数据
            var layEvent = obj.event; // 获得 lay-event 对应的值

            if (layEvent === 'details') { // 详情

                $('#headimg').attr('src', data.img || '');
                $('#username').text(data.username);
                $('#password').text(data.password);
                $('#phone').text(data.phone || '暂无');
                $('#regtime').text(data.regtime || '暂无');
                $('#birth').text(data.birth || '暂无');
                $('#status').text(data.status === 0 ? '正常' : '禁用');
                $('#salt').text(data.salt || '暂无');
                layer.open({
                    type: 1,
                    title: '用户详情',
                    area: ['600px', '400px'],
                    content: $('#userInfoDialog')
                });
            } else if (layEvent === 'del') { // 删除
                layer.confirm('确定删除此用户吗？', function (index) {
                    $.ajax({
                        url: '/user/delete',
                        type: 'GET',
                        data: { id: data.id },
                        success: function (res) {
                            if (res.success) {
                                layer.msg('删除成功');
                                table.reload('userlist');
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (layEvent === 'edit') { // 修改
                form.val('userEditForm', data);
                // 设置隐藏的 id 字段
                $("input[name='id']").val(data.id);
                layer.open({
                    type: 1,
                    title: '编辑用户信息',
                    area: ['600px', '500px'],
                    content: $('#userEditDialog')
                });
            }
        });

        // 监听提交表单
        form.on('submit(saveUserEdit)', function (data) {
            var editedData={
                id: data.field.id,
                username: data.field.username,
                password: data.field.password,
                phone: data.field.phone,
                regtime: data.field.regtime,
                birth: data.field.birth,
                status: data.field.status === 'on' ? 0 : 1,
                salt: data.field.salt
            }

            console.log(editedData)

            $.ajax({
                url: '/user/update',
                type: 'POST',
                data: JSON.stringify(editedData),
                contentType: 'application/json',
                success: function (res) {
                    if (res.code === 0) {
                        layer.msg('更新成功');
                        layer.closeAll('page');
                        table.reload('userlist');
                    } else {
                        layer.msg('更新失败');
                    }
                }
            });
            return false;
        });
    });
</script>

<!--操作栏自定义模板-->
<script id="rowTools" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="details">
            <i class="layui-icon layui-icon-list"></i>
            详情
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
        <button class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
                    修改
        </button>
    </div>
</script>
</body>
</html>